package com.mdp.plat.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.plat.entity.BankAccount;
import com.mdp.plat.service.BankAccountService;
import com.mdp.qx.HasRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * url编制采用rest风格,如对ADM.plat_bank_account 平台收付款账户的操作有增删改查,对应的url分别为:<br>
 *  新增: plat/bankAccount/add <br>
 *  查询: plat/bankAccount/list<br>
 *  模糊查询: plat/bankAccount/listKey<br>
 *  修改: plat/bankAccount/edit <br>
 *  删除: plat/bankAccount/del<br>
 *  批量删除: plat/bankAccount/batchDel<br>
 * 组织 com  顶级模块 mdp 大模块 plat 小模块 <br>
 * 实体 BankAccount 表 ADM.plat_bank_account 当前主键(包括多主键): card_account_id,platform_id; 
 ***/
@RestController("mdp.plat.bankAccountController")
@RequestMapping(value="/**/plat/bankAccount")
@Api(tags={"平台收付款账户操作接口"})
public class BankAccountController {
	
	static Logger logger =LoggerFactory.getLogger(BankAccountController.class);
	
	@Autowired
	private BankAccountService bankAccountService;
	 
		
 
	
	@ApiOperation( value = "查询平台收付款账户信息列表",notes=" ") 
	@ApiResponses({
		@ApiResponse(code = 200,response=BankAccount.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listBankAccount( @ApiIgnore @RequestParam Map<String,Object>  bankAccount){
		 
		
		IPage page=QueryTools.initPage(bankAccount);
		QueryWrapper<BankAccount> qw= QueryTools.initQueryWrapper(BankAccount.class,bankAccount);
		List<Map<String,Object>>	bankAccountList = bankAccountService.selectListMapByWhere(page,qw,bankAccount);
		
		return Result.ok().setData(bankAccountList).setTotal(page.getTotal());
		
	}
	
 
	

	@ApiOperation( value = "新增一条平台收付款账户信息",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200,response=BankAccount.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'},data:数据对象}")
	})

	@HasRole(roles = "superAdmin")
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Result addBankAccount(@RequestBody BankAccount bankAccount) {
		
		
		try{
		    boolean createPk=false;
			if(StringUtils.isEmpty(bankAccount.getCardAccountId())) {
			    createPk=true;
				//bankAccount.setCardAccountId(bankAccountService.createKey("cardAccountId"));
				return Result.error("receive-account-not-allow-empty","收款账号不得为空");
			}
			if(StringUtils.isEmpty(bankAccount.getPlatformId())) {
			    //createPk=true;
				//bankAccount.setPlatformId(bankAccountService.createKey("platformId"));
				bankAccount.setPlatformId("platform-001");
			}
			if(createPk==false){
                 if(bankAccountService.selectOneObject(bankAccount) !=null ){
					 return Result.error("receive-account-had-exists","收款账户重复，请修改编号再提交");
                }
            }
			bankAccountService.insert(bankAccount);
			return Result.ok().setData(bankAccount);
		}catch (BizException e) { 
			logger.error("",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("",e);
			return Result.error(e);
		}  
	}
	

	@ApiOperation( value = "删除一条平台收付款账户信息",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'}}")
	})

	@HasRole(roles = "superAdmin")
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public Result delBankAccount(@RequestBody BankAccount bankAccount){
		
		
		try{
			bankAccountService.deleteByPk(bankAccount);
			return Result.ok();
		}catch (BizException e) { 
			logger.error("",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("",e);
			return Result.error(e);
		}  
	}


	@ApiOperation( value = "根据主键修改一条平台收付款账户信息",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200,response=BankAccount.class, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'},data:数据对象}")
	})

	@HasRole(roles = "superAdmin")
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public Result editBankAccount(@RequestBody BankAccount bankAccount) {
		
		
		try{
			bankAccountService.updateByPk(bankAccount);
			return Result.ok().setData(bankAccount);
		}catch (BizException e) { 
			logger.error("",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("",e);
			return Result.error(e);
		}  
	}

	
	/**
	@ApiOperation( value = "根据主键列表批量删除平台收付款账户信息",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'}")
	}) 
	@RequestMapping(value="/batchDel",method=RequestMethod.POST)
	public Result batchDelBankAccount(@RequestBody List<BankAccount> bankAccounts) {
		
		 
		try{ 
			bankAccountService.batchDelete(bankAccounts);
		}catch (BizException e) { 
			logger.error("",e);
			return Result.error(e);
		}catch (Exception e) {
			logger.error("",e);
			return Result.error(e);
		}  
	} 
	*/
}
